package com.bai.Controller;

import com.bai.Service.FileService;
import com.bai.Utils.DtoResultUtil;
import com.bai.dto.PublicResponseBody;
import com.bai.dto.TreeDto;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/file")
public class FileController {
    @Autowired
    private FileService fileService;
    //存储文件的根路径
    private static String rootPath;

    public static String getRootPath() {
        return rootPath;
    }

    /**
     * 自由文件添加 可以自定义添加文件的文件名
     * @param path 文件路径
     * @param file 上传文件
     * @return
     * @throws IOException
     */
    @PostMapping("/addFile")
    @RequiresRoles(value={"admin","hr"},logical= Logical.OR)
    public PublicResponseBody addFile(String path,MultipartFile file) throws IOException {
        loadRootPath();
        if (path!=null&&!path.equals("")&&file!=null){
            PublicResponseBody res = fileService.addFile(rootPath +  path, file);
            if (res!=null){
                return res;
            }
            return DtoResultUtil.resultError();
        }
        return DtoResultUtil.resultParamError();
    }

    /**
     * 快速添加 不用填写上传文件名，可以为特定模块提供的文件上传接口，例如：简历上传、照片上传等
     * @return
     * @throws IOException
     */
//    @PostMapping("/quickAdd")
//    public PublicResponseBody quickAdd(String path,MultipartFile file) throws IOException {
//        loadRootPath();
//        if (path!=null&&!path.equals("")&&file!=null){
//            PublicResponseBody res = fileService.addFile("", rootPath +  path,  file);
//            if (res!=null){
//                return res;
//            }
//            return DtoResultUtil.resultError();
//        }
//        return DtoResultUtil.resultParamError();
//    }
    @RequiresRoles(value={"admin","hr"},logical= Logical.OR)
    @PostMapping("/mkdir")
    public PublicResponseBody mkdir(String name,String path){
        loadRootPath();
        if (name!=null&&!name.equals("")&&path!=null&&!path.equals("")){
            PublicResponseBody res = fileService.makeDir(name, rootPath+path);
            if (res!=null){
                return res;
            }
            return DtoResultUtil.resultError();
        }
        return DtoResultUtil.resultParamError();
    }
    @RequiresRoles(value={"admin","hr"},logical= Logical.OR)
    @GetMapping("/delFile")
    public PublicResponseBody delFile(Integer id,String path){
        loadRootPath();
        if (id!=null&&id>0&&path!=null&&!path.equals("")){
            PublicResponseBody res = fileService.delFile(id, rootPath.substring(0,rootPath.length()-7)+path);
            if (res!=null){
                return res;
            }
            return DtoResultUtil.resultError();
        }
        return DtoResultUtil.resultParamError();
    }
    @RequiresRoles(value={"admin","hr"},logical= Logical.OR)
    @GetMapping("/getList")
    public TreeDto getFileList(Integer nodeId){
        if (nodeId==null){
            return fileService.getFileList(1);
        }
        if (nodeId>0){
            return fileService.getFileList(nodeId);
        }
        Map<String,String> status=new HashMap();
        status.put("code","500");
        status.put("message","参数错误");
        return new TreeDto(status);
    }
    @RequiresRoles(value={"admin","hr"},logical= Logical.OR)
    @GetMapping("/getCount")
    public PublicResponseBody getFileCount(){
        return fileService.getFileCount();
    }

    public void loadRootPath(){
        if (rootPath==null){
            HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
            rootPath=request.getSession().getServletContext().getRealPath("/upload");
            new File(rootPath).mkdirs();
//            new File(rootPath+"/headImage");
//            new File(rootPath+"/resume");
        }
    }
}
